package com.shj.em.biz.sell.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.shj.em.biz.sell.entity.Order;
import com.shj.em.biz.sell.entity.OrderItem;
import com.shj.em.biz.sell.mapper.OrderItemMapper;
import com.shj.em.biz.sell.mapper.OrderMapper;
import com.shj.em.biz.sell.dto.QuotSearchDTO;
import com.shj.em.biz.sell.entity.QuotItem;
import com.shj.em.biz.sell.entity.Quotation;
import com.shj.em.biz.sell.mapper.QuotItemMapper;
import com.shj.em.biz.sell.mapper.QuotationMapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.plugins.Page;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.shj.em.util.DateUtils;
import com.shj.em.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


/**
 * 报价信息 服务实现类
 *
 * @author Shen Huang Jian
 * @since 2018-07-07
 */
@Service
@Transactional
public class QuotationServiceImpl extends ServiceImpl<QuotationMapper, Quotation> implements QuotationService {

    @Autowired
    QuotationMapper quotationMapper;

    @Autowired
    QuotItemMapper quotItemMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderItemMapper orderItemMapper;

    public Page<Quotation> queryQuotationPage(QuotSearchDTO searchDTO){
        Page<Quotation> page = new Page<>(searchDTO.getPageNo(), searchDTO.getPageSize());
        List<Quotation> list = quotationMapper.queryQuotationPage(page, searchDTO);
        return page.setRecords(list);
    }

    public synchronized Quotation createQuotation(Quotation quotation){
        if(StringUtils.isBlank(quotation.getQuotNo())){
            String quotNo = genQuotationNo(quotation.getQuotDt());
            quotation.setQuotNo(quotNo);
        }
        quotationMapper.insert(quotation);

        return quotationMapper.selectById(quotation.getId());
    }

    public synchronized String genQuotationNo(Date date){
        String prefix = DateUtils.format(date, "yyMMdd");
        List<Quotation> quotations = quotationMapper.findQuotationByDate(prefix);
        if(quotations == null || quotations.isEmpty()){
            return prefix + "01";
        }
        //已经按降序排列了
        int max = Integer.parseInt(quotations.get(0).getQuotNo());
        max++;
        return max + "";
    }


    /**
     * 返回创建的订单的主键
     * @param quotId
     * @return
     */
    public Long convert2Order(Long quotId){
        Quotation quotation = quotationMapper.selectById(quotId);
        Order order = new Order();
        order.setOrderNo(quotation.getQuotNo());
        order.setCustId(quotation.getCustId());
        order.setContactId(quotation.getContactId());
        order.setOrderDt(new Date());
        order.setSellMan(quotation.getSellMan());
        order.setFromQuot(1);

        //获得总价
        Wrapper<QuotItem> wrapper = new EntityWrapper();
        wrapper.eq("del_flag", 0)
                .eq("quot_id", quotId);

        List<QuotItem> items = quotItemMapper.selectList(wrapper);
        BigDecimal total = new BigDecimal("0.00");
        for(QuotItem item : items){
            total.add(item.getPrice().multiply(new BigDecimal(item.getNum())));
        }
        order.setTotalAmount(total);

        orderMapper.insert(order);
        Order db = orderMapper.findOrderByOrderNo(quotation.getQuotNo());

        //创建产品清单
        OrderItem oi;
        for(QuotItem item : items){
            oi = new OrderItem();
            oi.setOrderId(db.getId());
            oi.setMaterial(item.getMaterial());
            oi.setName(item.getName());
            oi.setNum(item.getNum());
            //oi.setPrice(item.getPrice());
            oi.setUnit(item.getUnit());
            orderItemMapper.insert(oi);
        }

        // 设置是否已经转换成订单
        quotation.setConvertFlag(1);
        quotation.setVersion(quotation.getVersion() + 1);
        quotationMapper.updateById(quotation);

        return db.getId();

    }

    public boolean isQuotNoExist(String quotNo, Long excludeId){
        Wrapper<Quotation> wrapper = new EntityWrapper<>();
        wrapper.eq("del_flag", 0).eq("quot_no", quotNo);
        if(excludeId != null){
            wrapper.ne("id", excludeId);
        }
        List<Quotation> list = quotationMapper.selectList(wrapper);
        return list != null && list.size() > 0;
    }
}
